package io.github.tuuzed.hydrogen.simple;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import io.github.tuuzed.hydrogen.Delete;

public class UserActivity extends AppCompatActivity {
    public static final String USER = "USER";

    private EditText et_name;
    private EditText et_sex;
    private EditText et_age;
    private EditText et_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        EditText et_id = (EditText) findViewById(R.id.et_id);
        et_name = (EditText) findViewById(R.id.et_name);
        et_sex = (EditText) findViewById(R.id.et_sex);
        et_age = (EditText) findViewById(R.id.et_age);
        et_password = (EditText) findViewById(R.id.et_password);
        Button btn_add = (Button) findViewById(R.id.btn_add);
        Button btn_del = (Button) findViewById(R.id.btn_del);

        final UserModel user = (UserModel) getIntent().getSerializableExtra(USER);
        if (user == null) {
            btn_add.setVisibility(View.VISIBLE);
            btn_del.setVisibility(View.GONE);
            btn_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UserModel user = new UserModel();
                    user.setAge(Integer.parseInt(et_age.getText().toString()));
                    user.setName(et_name.getText().toString());
                    user.setPassword(et_password.getText().toString());
                    user.setSex(et_sex.getText().toString());
                    user.save();
                    finish();
                }
            });

        } else {
            btn_del.setVisibility(View.VISIBLE);
            et_id.setText(user.getId() + "");
            et_name.setText(user.getName());
            et_sex.setText(user.getSex());
            et_age.setText(user.getAge() + "");
            et_password.setText(user.getPassword());
            btn_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    user.setAge(Integer.parseInt(et_age.getText().toString()));
                    user.setName(et_name.getText().toString());
                    user.setPassword(et_password.getText().toString());
                    user.setSex(et_sex.getText().toString());
                    user.save();
                    finish();
                }
            });
            btn_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    user.delete();
                    finish();
                }
            });
        }


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}
